package top.ashman.demo.springcloud.hystrix;

import java.io.IOException;
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.util.Optional;

/**
 * @author sunzhaojie
 * @date 2018-12-10
 */
public class MyUtil {
    public static HttpResponse<String> request(String uri) throws IOException, InterruptedException {
        HttpClient httpClient = HttpClient.newHttpClient();
        return httpClient.send(HttpRequest.newBuilder()
                .uri(URI.create(uri))
                .GET()
                .build(), HttpResponse.BodyHandlers.ofString());

    }

    public static void printResponse(HttpResponse<String> httpResponse) {
        Optional.ofNullable(httpResponse).ifPresent(response -> {
            System.out.println(response.body());
            System.out.println(response.statusCode());
            System.out.println(response.version());
            System.out.println(response.headers().map());
        });
    }
}
